package com.example.springboot.ytest.ytest;

import com.example.springboot.ytest.bean.AdaptCapability;
import com.example.springboot.ytest.bean.VehicleCapabilityElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

/**
 * @author yeyi
 * @date 2021-03-05
 **/
public class ReflectTest {
    private static final Logger logger = LoggerFactory.getLogger(ReflectTest.class);

    public static void main(String[] args){
        Field[] files = VehicleCapabilityElement.class.getDeclaredFields();
        try {
            for (Field f : files) {
                boolean assignableFromList = List.class.isAssignableFrom(f.getType());
                logger.info("是否属于List：{},{},{}", f.getName(), f.getType().getName(), assignableFromList);
                if (!assignableFromList) {
                    continue;
                }

                ParameterizedType pt = (ParameterizedType) f.getGenericType();
                Type[] aTypes = pt.getActualTypeArguments();
                if (null == aTypes || 0 == aTypes.length) {
                    continue;
                }
                Class clz = (Class) pt.getActualTypeArguments()[0];
                logger.info("List 中成员的类型：{}",clz);
                logger.info("List 中成员是否继承至：{}", AdaptCapability.class.isAssignableFrom(clz));

                // 插入子项所有属性
                Field[] subFileds = clz.getDeclaredFields();
                for (Field sf : subFileds) {
                    logger.info("子对象属性：{}", sf.getName());
                }
            }
        } catch (Exception e) {
            logger.error("getAllCapabilityNam err: ", e);
        }
    }
}
